package com.cindata

import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

@Secured(['isAuthenticated()'])
@Transactional(readOnly = true)
class AbnormalMortgageBatchFlowStageConditionController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond AbnormalMortgageBatchFlowStageCondition.list(params), model:[abnormalMortgageBatchFlowStageConditionCount: AbnormalMortgageBatchFlowStageCondition.count()]
    }

    def show(AbnormalMortgageBatchFlowStageCondition abnormalMortgageBatchFlowStageCondition) {
        respond abnormalMortgageBatchFlowStageCondition
    }

    def create() {
      def targetUri = request.getHeader("referer")
      params['targetUri'] = targetUri
      def flowStage = AbnormalMortgageBatchFlowStage.findById(params['flowStage'])
      def flowStages = AbnormalMortgageBatchFlowStage.findAllByBatch(flowStage?.batch)
        respond new AbnormalMortgageBatchFlowStageCondition(params), model: [abnormalMortgageBatchFlowStages: flowStages]
    }

    @Transactional
    def save(AbnormalMortgageBatchFlowStageCondition abnormalMortgageBatchFlowStageCondition) {
        if (abnormalMortgageBatchFlowStageCondition == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (abnormalMortgageBatchFlowStageCondition.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond abnormalMortgageBatchFlowStageCondition.errors, view:'create'
            return
        }

        abnormalMortgageBatchFlowStageCondition.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'abnormalMortgageBatchFlowStageCondition.label', default: 'AbnormalMortgageBatchFlowStageCondition'), abnormalMortgageBatchFlowStageCondition.id])
                // redirect abnormalMortgageBatchFlowStageCondition
                redirect url: params['targetUri']
            }
            '*' { respond abnormalMortgageBatchFlowStageCondition, [status: CREATED] }
        }
    }

    def edit(AbnormalMortgageBatchFlowStageCondition abnormalMortgageBatchFlowStageCondition) {
      def targetUri = request.getHeader("referer")
      params['targetUri'] = targetUri
      def flowStages = AbnormalMortgageBatchFlowStage.findAllByBatch(abnormalMortgageBatchFlowStageCondition?.flowStage?.batch)
        respond abnormalMortgageBatchFlowStageCondition, model: [abnormalMortgageBatchFlowStages: flowStages]
    }

    @Transactional
    def update(AbnormalMortgageBatchFlowStageCondition abnormalMortgageBatchFlowStageCondition) {
        if (abnormalMortgageBatchFlowStageCondition == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (abnormalMortgageBatchFlowStageCondition.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond abnormalMortgageBatchFlowStageCondition.errors, view:'edit'
            return
        }

        abnormalMortgageBatchFlowStageCondition.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'abnormalMortgageBatchFlowStageCondition.label', default: 'AbnormalMortgageBatchFlowStageCondition'), abnormalMortgageBatchFlowStageCondition.id])
                // redirect abnormalMortgageBatchFlowStageCondition
                redirect url: params['targetUri']
            }
            '*'{ respond abnormalMortgageBatchFlowStageCondition, [status: OK] }
        }
    }

    @Transactional
    def delete(AbnormalMortgageBatchFlowStageCondition abnormalMortgageBatchFlowStageCondition) {
      def targetUri = request.getHeader("referer")

        if (abnormalMortgageBatchFlowStageCondition == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        abnormalMortgageBatchFlowStageCondition.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'abnormalMortgageBatchFlowStageCondition.label', default: 'AbnormalMortgageBatchFlowStageCondition'), abnormalMortgageBatchFlowStageCondition.id])
                // redirect action:"index", method:"GET"
                redirect url: targetUri
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'abnormalMortgageBatchFlowStageCondition.label', default: 'AbnormalMortgageBatchFlowStageCondition'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
